package cn.com.huzo.service.tech.impl;

import cn.com.huzo.common.util.ApplicationConfiguration;
import cn.com.huzo.model.entities.tech.PieceYardageHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.tech.PieceYardageHeaderRepository;
import cn.com.huzo.repository.tech.PieceYardageLinesProductColourRepository;
import cn.com.huzo.repository.tech.PieceYardageLinesProductSizeRepository;
import cn.com.huzo.repository.tech.PieceYardageLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.tech.PieceYardageHeaderManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单耗信息管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PieceYardageHeaderManager")
public class PieceYardageHeaderManagerImpl extends GenericManagerImpl<PieceYardageHeaderEntity, Long>
        implements PieceYardageHeaderManager {

    private final String rootNodeText = (String) ApplicationConfiguration.getProperty("Catalogue_PieceYardage");

    @Resource(name = "PieceYardageHeaderRepository")
    private PieceYardageHeaderRepository _dao;

    @Resource(name = "PieceYardageLinesRepository")
    private PieceYardageLinesRepository _dao_lines;

    @Resource(name = "PieceYardageLinesProductColourRepository")
    private PieceYardageLinesProductColourRepository _dao_lines_product_colour;

    @Resource(name = "PieceYardageLinesProductSizeRepository")
    private PieceYardageLinesProductSizeRepository _dao_lines_product_size;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Long headerKey) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("HeaderEntity", _dao.fetchByKey(rootNodeText, headerKey));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(headerKey));
        _result.put("ProductColourNode", _dao_lines_product_colour.fetchByHeaderKey(headerKey));
        _result.put("ProductSizeNode", _dao_lines_product_size.fetchByHeaderId(headerKey));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<PieceYardageHeaderEntity> fetchByStyleNo(Map<String, Object> criterion) throws Exception {
        List<PieceYardageHeaderEntity> itemList = _dao.fetchByStyleNo(criterion);
        return itemList == null ? new ArrayList() : itemList;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<PieceYardageHeaderEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<PieceYardageHeaderEntity> paginator = new Paginator<>(itemList, offset, (Integer) criterion.get("Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> add(PieceYardageHeaderEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long key = _dao.insert(instance);
        if (key != null && key > 0) {
            // 根据款号从生产指示书得到成品色
            _dao_lines_product_colour.insertByStyleNo(key, instance.getProductId());
            // 根据款号从生产指示书得到成品尺码
            _dao_lines_product_size.insertByStyleNo(key, instance.getProductId());
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "技术管理→单耗表", "款号：" + instance.getStyleNo(), "增加均码单耗");
        }

        _result.put("HeaderKey", key);
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(key));
        _result.put("ProductColourNode", _dao_lines_product_colour.fetchByHeaderKey(key));
        _result.put("ProductSizeNode", _dao_lines_product_size.fetchByHeaderId(key));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(PieceYardageHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao.update(instance);
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(instance.getUpdatedBy(), "技术管理→单耗主表", "款号：" + instance.getStyleNo(), "更新均码单耗");
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "HeaderId"));
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "技术管理→单耗表", "款号：" + MapUtils.getString(criterion, "DeletedStyleNo"),
                    "删除均码单耗");
        }

        return fetchForPage(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer updateGradedBy(PieceYardageHeaderEntity instance) throws Exception {
        return _dao.updateGradedBy(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer batchChangeCatalogue(List<PieceYardageHeaderEntity> itemList) throws Exception {
        return _dao.batchChangeCatalogue(itemList);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchPrintData(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();

        _result.put("HeaderEntity", _dao.fetchByKey(rootNodeText, headerId));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(headerId));
        _result.put("ProductColourNode", _dao_lines_product_colour.fetchByHeaderKey(headerId));

        return _result;
    }
}
